package workspace.teaweb.Logistics.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import springfox.documentation.annotations.ApiIgnore;
import workspace.teaweb.CRM.pojo.vo.CRMVO;
import workspace.teaweb.Logistics.mappers.LogisticsMapper;
import workspace.teaweb.Logistics.mappers.OrderMapper;
import workspace.teaweb.Logistics.mappers.WarehouseMapper;
import workspace.teaweb.Logistics.pojo.dto.*;
import workspace.teaweb.Logistics.pojo.entity.Logistics;
import workspace.teaweb.Logistics.pojo.entity.Order;
import workspace.teaweb.Logistics.pojo.entity.Warehouse;
import workspace.teaweb.Logistics.pojo.vo.orderVO;
import workspace.teaweb.Logistics.service.LogisticsService;
import workspace.teaweb.User.pojo.vo.UserVO;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import workspace.teaweb.base.config.ThreadPoolConfig;

@Slf4j
@Service
public class Logisticsimpl implements LogisticsService {
    @Autowired
    LogisticsMapper logisticsMapper;

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    WarehouseMapper warehouseMapper;

    @Autowired
    @Qualifier("taskExecutor1")
    private ExecutorService taskExecutor;

    @Override
    public void insertlogistics(Logistics logistics) {
        logisticsMapper.insertlogistics(logistics);
        log.debug("物流信息添加:{}",logistics);
    }

    @Override
    public void updateGetlogistics(LogisticsGetDto logistics) {
        logisticsMapper.updateGetlogistics(logistics);
        log.debug("物流信息修改:{}",logistics);
        if(logistics.getStatus().equals("待签收"))
        {
            Order order =selectOrderByLogisticsid(Math.toIntExact(logistics.getId())) ;
            CRMupdateOrderStatus(order);
        }
    }

    @Override
    public void updatePostlogistics(LogisticsPostDto logistics) {
        logisticsMapper.updatePostlogistics(logistics);
        log.debug("物流信息修改:{}",logistics);
    }
    @Override
    public Logistics selectlogisticsById(Long id) {
        Logistics logistics = logisticsMapper.selectlogisticsById(id);
        log.debug("物流信息查询:{}",logistics);
        return logistics;
    }

    @Override
    public List<Logistics> selectlogisticsByCRMid(Logistics logistics) {
        List<Logistics> logisticsvo=logisticsMapper.selectlogisticsByCRMid(logistics);
        log.debug("物流信息查询:{}",logistics);

        return logisticsvo;
    }

    @Override
    public Logistics selectlogisticsByPostWarehouseidAndStatus(Long PostWarehouseid, String status) {
        Logistics logistics = logisticsMapper.selectlogisticsByPostWarehouseidAndStatus(PostWarehouseid,status);
        log.debug("物流信息查询:{}",logistics);
        return logistics;
    }

    @Override
    public Logistics selectlogisticsByGetWarehouseidAndStatus(Long GetWarehouseid, String status) {
        Logistics logistics = logisticsMapper.selectlogisticsByGetWarehouseidAndStatus(GetWarehouseid,status);
        log.debug("物流信息查询:{}",logistics);
        return logistics;
    }

    @Override
    public void CRMinsertOrder(Order order, Logistics logistics) {
        orderMapper.CRMinsertOrder(order);
        insertlogistics(logistics);
        log.debug("订单信息查询:{}",order);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void UserinsertOrder(Order order) {
        Logistics logistics=new Logistics();
        logistics.setCRMid(0L);
        logistics.setPostTime(new Date());
        logistics.setUpdateTime(new Date());
        logistics.setStatus("待发货");
        logistics.setPostWarehouseid(0L);
        logistics.setGetWarehouseid(0L);
        insertlogistics(logistics);


        order.setLogisticsid(logistics.getId());
        order.setCreateTime(new Date());
        order.setUpdateTime(new Date());
        System.out.println(order);
        order.setStatus("待发货");
        orderMapper.UserinsertOrder(order);
        log.debug("订单信息查询:{}",order);

        taskExecutor.submit(() -> {
            System.out.println("异步任务执行");
        WarehouseDto2 warehouseDto2=new WarehouseDto2();
            System.out.println("warehouseDto2");
            System.out.println(order.getMerchantid());
            Warehouse warehouse= warehouseMapper.selectWarehouseById(order.getMerchantid());
            System.out.println(warehouse);

        BeanUtils.copyProperties(warehouse,warehouseDto2);
            System.out.println(warehouseDto2);
        warehouseDto2.setNumber(warehouseDto2.getNumber()-order.getNum());
            System.out.println(warehouseDto2);
        AutoupdateWarehouse(warehouseDto2);
        });
    }

    @Override
    public void CRMupdateOrderStatus(Order order) {
        orderMapper.CRMupdateOrderStatus(order);
        log.debug("订单信息查询:{}",order);

    }

    @Override
    public void UserupdateOrderStatus(Long id) {
        Order order = new Order();
        order.setId(id);
        order.setStatus("已确认收货");
        order.setUpdateTime(new Date());
        orderMapper.UserupdateOrderStatus(order);
        log.debug("订单信息查询:{}",id);
    }

    @Override
    public void AdminupdateOrder(Order order) {
        System.out.println("order订单是："+order);
        orderMapper.AdminupdateOrder(order);
        System.out.println("order订单是："+order);
    }

    @Override
    public List<Order> CRMselectOrderById(OrderDto order,CRMVO CRMVo) {
        int CRMid = Math.toIntExact(CRMVo.getId());
        List<Order> ordervo = orderMapper.CRMselectOrderById(order,CRMid);
        log.debug("订单信息查询:{}",ordervo);
        return ordervo;
    }

    @Override
    public List<orderVO> UserselectOrderById(Long id, UserVO userVO) {
        Long userid = id;
        List<orderVO> ordervo = orderMapper.UserselectOrderById(userid);
        log.debug("订单信息查询:{}",ordervo);
        return ordervo;
    }

    @Override
    public List<orderVO> AdminselectOrderById(Long id,UserVO userVO) {
        Long adminId = id;
        List<orderVO> ordervo = orderMapper.AdminselectOrderById(adminId);
        log.debug("订单信息查询:{}",ordervo);
        return ordervo;
    }

    @Override
    public List<orderVO> AdminselectOrderByOrderId(Long orderId, UserVO userVO) {
        List<orderVO> ordervo = orderMapper.AdminselectOrderByOrderId(orderId);
        log.debug("订单信息查询:{}",ordervo);
        return ordervo;
    }

    @Override
    public Order selectOrderByLogisticsid(int logisticsid) {
        Order order = orderMapper.selectOrderByLogisticsid(logisticsid);
        log.debug("订单信息查询:{}",order);
        return order;
    }



    @Override
    public void insertWarehouse(Warehouse warehouse, CRMVO CRMVo) {
        int CRMid = Math.toIntExact(CRMVo.getId());
            warehouseMapper.insertWarehouse(warehouse, (long) CRMid);
            log.debug("仓库信息查询:{}",warehouse);
    }

    @Override
    public void AutoinsertWarehouse(Warehouse warehouse) {
        warehouseMapper.AutoinsertWarehouse(warehouse);
        log.debug("仓库信息查询:{}",warehouse);
    }

    @Override
    public void updateWarehouse(WarehouseDto2 warehouse, CRMVO CRMVo) {
        taskExecutor.submit(() -> {
        int CRMid = Math.toIntExact(CRMVo.getId());
        warehouseMapper.updateWarehouse(warehouse, (long) CRMid);
        log.debug("仓库信息查询:{}",warehouse);
        });
    }

    @Override
    public void AutoupdateWarehouse(WarehouseDto2 warehouse) {
        warehouseMapper.AutoupdateWarehouse(warehouse);
        log.debug("仓库信息查询:{}",warehouse);
    }

    @Override
    public void UserupdateWarehouse(Warehouse warehouse) {
        warehouseMapper.UserupdateWarehouse(warehouse);
        log.debug("仓库信息查询:{}",warehouse);
    }

    @Override
    public List<Warehouse> selectAllWarehouse(WarehouseDto warehouse, CRMVO CRMVo)
    {
        int CRMid = Math.toIntExact(CRMVo.getId());
        List<Warehouse> warehousevo = warehouseMapper.selectAllWarehouse(warehouse, (long) CRMid);
        log.debug("订单信息查询:{}",warehousevo);
        return warehousevo;
    }


    @Override
    public void deleteWarehouseById(int id) {
        warehouseMapper.deleteWarehouseById(id);
        log.debug("仓库信息查询:{}",id);
    }


}
